//
//  YDTabbar.swift
//  YDWeibo01
//
//  Created by suibian on 16/8/9.
//  Copyright © 2016年 iitcast. All rights reserved.
//

import UIKit
//声明协议
protocol YDTabbarDelegate:NSObjectProtocol{

//声明方法
    func tabbarButtonClick()

}

class YDTabbar: UITabBar {
    
//    声明一个代理
    weak var ydDelegate:YDTabbarDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    监听方法
    func buttonClick(){
//    print("撰写按钮点击")
        ydDelegate?.tabbarButtonClick()
    
    
    }
    
//    设置视图
    private func setupUI(){
//    添加控件
        addSubview(composeButton)
    
    }
    //    设置整个tabbar的frame
    override func layoutSubviews() {
        super.layoutSubviews()
        //      该tabbar控件是系统的 拿不到  可以通过视图层级关系知道对象的名称 通过遍历整个tabbar控件 来设置frame
        
        
        let childW = self.frame.width * 0.2
        var index: CGFloat = 0
        for value in self.subviews{
            // NSClassFromString 通过字符串转class
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                value.frame.size.width = childW
                value.frame.origin.x = index * childW
                index++
                if index == 2{
                index++
                }
                
            }
            
            
        }
        composeButton.center.x = self.frame.width * 0.5
        composeButton.center.y = self.frame.height * 0.5
        
    }
    

    
    
//    懒加载控件
    private lazy var composeButton:UIButton = {
    let button = UIButton()
        
//        撰写按钮点击事件
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
//        设置背景图片
        button.setBackgroundImage(UIImage(imageLiteral: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
//        设置图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
//        设置frame
        button.sizeToFit()
        
        return button
        
    
    
    }()
    

}
